package com.dmz.workflow.controller;

import java.util.List;
import javax.validation.Valid;

import com.echronos.commons.Result;
import com.echronos.commons.annotation.Permission;
import com.dmz.workflow.constant.PermissionConstants;
import com.dmz.workflow.service.FlowableFormService;
import com.dmz.workflow.vo.FormInfoGroupVo;
import com.dmz.workflow.vo.FormInfoVo;
import com.dmz.workflow.vo.ProcessSetupVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * 流程定义后台管理
 **/
@Slf4j
@RestController
@RequestMapping("/v1/flowable/form")
public class ProcessDesignerController {

    @Autowired
    private FlowableFormService flowableFormService;

    /**
     * 表单列表展示（根据公司的列表展示，是拥有管理员权限的）
     * endpoint 1 展现端 2 管理端 默认管理端
     **/
    @GetMapping(value = "/list")
    public Result<IPage<FormInfoGroupVo>> list(String keyword, Integer endpoint) {
        return Result.build(flowableFormService.listProcess(keyword, endpoint));
    }

    /**
     * 根据流程定义ID查看流程定义信息
     **/
    @GetMapping(value = "/queryFormById")
    public Result<ProcessSetupVo> queryFormById(@RequestParam String processDefinitionId) {
        return Result.build(flowableFormService.queryFormById(processDefinitionId));
    }

    /**
     * 新增流程表单
     **/
    @PostMapping(value = "/save")
    @Permission(PermissionConstants.PROCESS_SETTING_ADVANCE)
    public Result<?> saveForm(@RequestBody FormInfoVo flowableForm) {
        return flowableFormService.saveForm(flowableForm);
    }

    /**
     * 修改流程表单
     **/
    @PutMapping(value = "/update")
    @Permission(PermissionConstants.PROCESS_SETTING_ADVANCE)
    public Result<?> updateFormById(@Valid @RequestBody FormInfoVo flowableForm) {
        return flowableFormService.updateFormById(flowableForm);
    }

    /**
     * 根据processDefinitionId删除流程表单（可批量）
     **/
    @DeleteMapping(value = "/delete")
    @Permission(PermissionConstants.PROCESS_DELETE_SORT)
    public Result<?> delete(@RequestParam List<String> processDefinitionIds) {
        flowableFormService.removeFormByIds(processDefinitionIds);
        return Result.build();
    }

    /**
     * 分组内表单列表排序
     **/
    @PostMapping(value = "/updateFromSort")
    @Permission(PermissionConstants.PROCESS_MODEL_SORT)
    public Result<?> updateFromSort(@RequestBody List<FormInfoVo> infoVos) {
        flowableFormService.updateFromSort(infoVos);
        return Result.build();
    }

    /**
     * 获取该公司下面的所有有效流程名称列表
     *
     * @return 流程名列表
     */
    @GetMapping(value = "/queryValidProcessNames")
    public Result<List<String>> queryValidProcessNames() {
        return Result.build(flowableFormService.queryValidProcessNames());
    }

}
